﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kohvik.Kohvikuteenus;

namespace Kohvik
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        private ServiceClient service = new ServiceClient();
        private static int entityId;
        private static KommentaarType kommentaarType;

        private static LiteralControl name_label = new LiteralControl("<strong>Nimi:</strong><br />");
        private static LiteralControl comment_label = new LiteralControl("<strong>Kommentaar:</strong><br /><br />");

        private const String TWO_NEW_LINES = "<br /><br />";

        private const int CONFIRM_IMAGE_WIDTH = 85,
                          CONFIRM_PANEL_WIDTH = 260,
                          MINIMAL_AUTHOR_LENGTH = 2,
                          MINIMAL_COMMENT_LENGTH = 2;

        private static Label USER_OUTPUT = new Label();

        private static Image confirmImage = new Image()
        {
            ImageUrl = "images/captcha.jpg",
            ImageAlign = ImageAlign.Right,
            Width = CONFIRM_IMAGE_WIDTH
            
        };

        private static TextBox textBox_author = new TextBox()
        {
            Height = 16,
            Width = 157,
        };

        private static TextBox textBox_confirmation = new TextBox()
        {
            Height = 16,
            Width = 157,
        };

        private static TextBox textBox_comment = new TextBox()
        {
            Height = 126,
            Width = 528,
            Rows = 5,
            TextMode = TextBoxMode.MultiLine

        };

        private static Button button_postComment = new Button()
        {
            Text = "Lisa kommentaar",
            Height = 22,
            Width = 206,
        };

        private static Panel confirm_panel = new Panel()
        {
            Width = CONFIRM_PANEL_WIDTH,
            
        };

        protected void Page_Load(object sender, EventArgs e)
        {

            String type = Request["tyyp"];
            String stringId = Request["id"];


            if (!Int32.TryParse(stringId, out entityId) || !Enum.TryParse(type, out kommentaarType))
                return;

            List<EntityKommentaar> comments = service.getEntityKommentaarFor(kommentaarType, entityId);

            if(comments != null)
            foreach(EntityKommentaar c in comments)
                PlaceHolder_DisplayComments.Controls.Add(new LiteralControl(@"<p style='font-size:16px; margin-top:30px; margin-left:20px; background-color:#757575; '>" + c.Autor + " | " + c.Aeg.Day + "-" + c.Aeg.Month + "-" + c.Aeg.Year + " | " + c.Aeg.TimeOfDay + "<br />" + c.Tekst + "</p>"));
            
            createCommentInput();
        }


        private void createCommentInput()
        {

            PlaceHolder_AddComments.Controls.Add(USER_OUTPUT);
            PlaceHolder_AddComments.Controls.Add(new LiteralControl("<br />"));

            confirm_panel.Controls.Add(confirmImage);

            PlaceHolder_AddComments.Controls.Add(name_label);
            PlaceHolder_AddComments.Controls.Add(confirm_panel);

            PlaceHolder_AddComments.Controls.Add(textBox_author);
            PlaceHolder_AddComments.Controls.Add(new LiteralControl(TWO_NEW_LINES));

            PlaceHolder_AddComments.Controls.Add(new LiteralControl("<i>Kes on pildil ?</i><br />"));
            PlaceHolder_AddComments.Controls.Add(textBox_confirmation);
            PlaceHolder_AddComments.Controls.Add(new LiteralControl(TWO_NEW_LINES));

            PlaceHolder_AddComments.Controls.Add(comment_label);
            PlaceHolder_AddComments.Controls.Add(textBox_comment);
            PlaceHolder_AddComments.Controls.Add(new LiteralControl(TWO_NEW_LINES));
            PlaceHolder_AddComments.Controls.Add(button_postComment);

            button_postComment.Click += new EventHandler(button_postComment_Click);
            

        }


        private void button_postComment_Click(object sender, EventArgs e)
        {
            // Self note: Postpacki jama eemaldada
            bool isConfirmCorrect = false,
                isAuthorCorrect = false,
                isCommentCorrect = false;

            String pedokaruConfirm = textBox_confirmation.Text.ToUpper();

            String author = textBox_author.Text;
            String comment = textBox_comment.Text;
            DateTime timeOfPost = System.DateTime.UtcNow.AddHours(3);


            if (pedokaruConfirm.Equals("MÕMMI") || pedokaruConfirm.Equals("KARU"))
                isConfirmCorrect = true;
            if (author.Length >= MINIMAL_AUTHOR_LENGTH)
                isAuthorCorrect = true;
            if (comment.Length > MINIMAL_COMMENT_LENGTH)
                isCommentCorrect = true;

            if (isConfirmCorrect && isAuthorCorrect && isCommentCorrect)
            {
                service.addEntityKommentaarFor(kommentaarType, entityId, comment, author, timeOfPost);
                textBox_author.Text = "";
                textBox_comment.Text = "";
                textBox_confirmation.Text = "";
                USER_OUTPUT.Text = "";
            }
            else
            {
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(280);

                if (!isAuthorCorrect)
                    stringBuilder.Append("Lahter 'Nimi' peab olema vähemalt " + MINIMAL_AUTHOR_LENGTH + " tähemärki pikk; <br />");
                if (!isConfirmCorrect)
                    stringBuilder.Append("Kontrollige üle lahter: 'Kes on pildil'; <br />");
                if (!isCommentCorrect)
                    stringBuilder.Append("Kommentaar ei ole piisavalt sisukas (Vähemalt " + MINIMAL_COMMENT_LENGTH + " tähemärki) ");

                USER_OUTPUT.Text = stringBuilder.ToString();
            }

            Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
        }
    }
}